package com.gopay.bis.spdb.ebank.operation;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.spdb.ebank.SpdbMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.ICertHelper;

public class SpdbB2cQuery {
	/** 签名实现类 */
	private ICertHelper spdbB2cCertHelper;
	/** HTTP实现类 */
	private HttpTransport httpTransport;
	private SpdbBuildMessage spdbBuildMessage;
	private SpdbB2cParseMessage spdbB2cParseMessage;
	/**
	 * 订单查询地址
	 */

	public Object query(SpdbMessage message) throws GopayException {
		try {
			String signMsg = (String) this.spdbB2cCertHelper.sign(message);
			message.setSignMessage(signMsg);
			// 构建xml
			String buildMsg = spdbBuildMessage.buildQueryMessage(message);
			BankLogUtils.printB2CQuerySendLog(message, buildMsg);
			httpTransport.setUrl(message.getQueryUrl());
			httpTransport.setSendEncoding("GBK");
			Object obj = (Object) this.httpTransport.submit(buildMsg.getBytes()); // 发送银行
			message = spdbB2cParseMessage.parseSpdbMessage(obj);// 解析银行返回参数
			BankLogUtils.printB2CQueryBlackLog(message, ToStringBuilder.reflectionToString(message));
			return message;
		} catch (Exception e) {
			BankLogUtils.printB2CQueryExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
		}
	}

	public void setSpdbB2cCertHelper(ICertHelper spdbB2cCertHelper) {
		this.spdbB2cCertHelper = spdbB2cCertHelper;
	}

	public void setHttpTransport(HttpTransport httpTransport) {
		this.httpTransport = httpTransport;
	}

	public void setSpdbBuildMessage(SpdbBuildMessage spdbBuildMessage) {
		this.spdbBuildMessage = spdbBuildMessage;
	}

	public void setSpdbB2cParseMessage(SpdbB2cParseMessage spdbB2cParseMessage) {
		this.spdbB2cParseMessage = spdbB2cParseMessage;
	}


}
